﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DigitalSolutionLibrary.SqlDatabase;

namespace DigitalSolutionManagement
{
    public partial class frmListProduct : BaseFormList
    {
        /*
        private Thread th;
        private delegate void DelegateUpdateUI(int totalPage, DataSet ds);

        private void LoadProduct(int page)
        {
            Parameter par = new Parameter("CurPage", page, SqlDbType.Int);
            ParameterStock stock = new ParameterStock();
            stock.AddParamter(par);

            par = new Parameter("RowPerPage", Program.RowPerPage, SqlDbType.Int);
            stock.AddParamter(par);

            par = new Parameter("TotalPage", null, SqlDbType.Int, ParameterDirection.Output);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("GetProductList", stock, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                DataSet ds = (DataSet)obj;
                int totalPage = (int)stock.GetAtPosition(2).ValueOut;
                DelegateUpdateUI deleg = new DelegateUpdateUI(UpdateUI);
                Invoke(deleg, new object[] { totalPage, ds });
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến CSDL, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void UpdateUI(int totalPage, DataSet ds)
        {
            modifiedDataGridView1.DataSource = ds.Tables[0];
            paging1.TotalPage = totalPage;
            listPanel1.EnableDelete = listPanel1.EnableEdit = listPanel1.EnableView = modifiedDataGridView1.SelectedRows.Count > 0;
            processing1.HideProcessing();
        }

        private void DelProduct(int id)
        {
            Parameter par = new Parameter("ProId", id, SqlDbType.Int);
            ParameterStock stock = new ParameterStock();
            stock.AddParamter(par);

            par = new Parameter("RowPerPage", Program.RowPerPage, SqlDbType.Int);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("DeleteProduct", stock, DatabaseWrapper.ReturnType.FIRST_CELL);
            if (obj != null)
            {
                int tp = (int)obj;
                if (paging1.CurrentPage > tp)
                {
                    paging1.CurrentPage = tp;
                    paging1.TotalPage = tp;
                    LoadProduct(tp);
                }
                else
                {
                    LoadProduct(paging1.CurrentPage);
                }
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến CSDL, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        */
        public frmListProduct()
        {
            InitializeComponent();

            LoadDataStore = "GetProductList";
            EditForm = new frmEditProduct();
            DeleteItemStore = "DeleteProduct";
            RptTye = frmViewReport.ReportType.VIEW_PRO;
        }

        /*
        private void frmListProduct_Load(object sender, EventArgs e)
        {
            //processing1.ShowProcessing();
            //th = new Thread(() => LoadProduct(1));
            //th.Start();
        }

        private void listPanel1_AddClick(object sender, EventArgs e)
        {
            //frmEditProduct frmEditPro = new frmEditProduct(0, BaseFormEdit.OpenMode.NEW);
            //if (frmEditPro.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    paging1.CurrentPage = 1;
            //    processing1.ShowProcessing();
            //    th = new Thread(() => LoadProduct(1));
            //    th.Start();
            //}
        }

        private void listPanel1_DeleteClick(object sender, EventArgs e)
        {
            //string proName = modifiedDataGridView1.SelectedRows[0].Cells["ColName"].Value.ToString();
            //DialogResult rs = MessageBox.Show("Bạn có chắc muốn xóa sản phẩm \""+proName+"\"?","DSCo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (rs == System.Windows.Forms.DialogResult.Yes)
            //{
            //    int proId = (int)modifiedDataGridView1.SelectedRows[0].Cells["ColId"].Value;
                
                
            //    //processing1.ShowProcessing();
            //    //th = new Thread(() => DelProduct(proId));
            //    //th.Start();
            //}
        }

        private void listPanel1_EditClick(object sender, EventArgs e)
        {
            //int proId = (int)modifiedDataGridView1.SelectedRows[0].Cells["ColId"].Value;
            //EditForm = new frmEditProduct(proId, BaseFormEdit.OpenMode.EDIT);
            //frmEditProduct frmEditPro = new frmEditProduct(proId, BaseFormEdit.OpenMode.EDIT);
            //if (frmEditPro.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    processing1.ShowProcessing();
            //    th = new Thread(() => LoadProduct(paging1.CurrentPage));
            //    th.Start();
            //}
        }

        private void listPanel1_ViewClick(object sender, EventArgs e)
        {
            //int proId = (int)modifiedDataGridView1.SelectedRows[0].Cells["ColId"].Value;
            //frmViewReport frmViewRpt = new frmViewReport(frmViewReport.ReportType.VIEW_PRO);
            //frmViewRpt.ProId = proId;
            //frmViewRpt.Show();
        }

        private void paging1_PageChanged(object sender, EventArgs e)
        {
            //processing1.ShowProcessing();
            //th = new Thread(() => LoadProduct((int)sender));
            //th.Start();
        }

        */
    }
}
